import json
import os

from PyQt5 import QtWidgets
from PyQt5.QtCore import QStandardPaths

'''
配置
'''


class YqConfig:
    def __init__(self):
        # 配置文件位置：C:\Users\xxxx\Documents\YqConfig.json
        self.config_file_path = f"{QStandardPaths.writableLocation(QStandardPaths.DocumentsLocation)}/YqConfig.json"
        self.username = ""
        self.token = ""
        # 文章存储地址
        self.doc_path = ""
        # 图片存储地址
        self.pic_path = ""

    def save_config(self):
        config_data = {"username": self.username, "token": self.token, "doc_path": self.doc_path,
                       "pic_path": self.pic_path}
        with open(self.config_file_path, "w", encoding="utf-8") as json_file:
            json.dump(config_data, json_file, indent=4, ensure_ascii=False)

    def get_config(self):
        if os.path.isfile(self.config_file_path):
            with open(self.config_file_path, encoding="utf-8") as json_file:
                config_data = json.load(json_file)
                return config_data

    def choose_doc_path(self, ui):
        self.doc_path = QtWidgets.QFileDialog.getExistingDirectory(
            None, "选取文章存储文件夹", QStandardPaths.writableLocation(QStandardPaths.DesktopLocation))
        ui.lineEdit_doc_save.setText(self.doc_path)

    def choose_pic_path(self, ui):
        self.pic_path = QtWidgets.QFileDialog.getExistingDirectory(
            None, "选取图片存储文件夹", QStandardPaths.writableLocation(QStandardPaths.DesktopLocation))
        ui.lineEdit_pic_save.setText(self.pic_path)
